
class Socket1 {
    _subscribeMsgList = {}
    // defalutSubscribe = null

    constructor(){
        this.socket = null; // 实列
        this.url =  null; // url 
        this.heartTime = 8000
        this.lastHeartBeat = 0; // 上一次消息时间
        this.connectTimer = null; // 
        this.isPauseConnet = null; // 是否需要暂停链接
    }

    connect(url){
        this.url = url
        this.socket = new WebSocket(this.url)
        this.lastHeartBeat = new Date().getTime()
        const _this = this
        if(this.socket) {
            this.socket.onopen = this.socketopen.bind(this)
            this.socket.onerror = this.socketonerror.bind(this)
            this.socket.onclose = this.socketclose.bind(this)
            this.socket.onmessage = e => {
                
                let data = JSON.parse(e.data)
                _this.lastHeartBeat = new Date().getTime()
                _this._publishMsg(data, e)
            }
        }
    }
    // connect(){
    //     this._init()
    // }
    socketopen(){
        console.log("socketopen")
        clearInterval(this.connectTimer)
        this.isPauseConnet = true
        this.connectTimer = null
        this.connectTimer = setInterval(
            ()=>{this.checkConnect()}, this.heartTime)
    }

    socketonerror(callBack){
        console.log(callBack)
        if(this.socket && this.socket.readyState){
            // 0 (WebSocket.CONNECTING) 正在链接中
            // 1 (WebSocket.OPEN) 已经链接并且可以通讯
            // 2 (WebSocket.CLOSING) 连接正在关闭
            // 3 (WebSocket.CLOSED) 连接已关闭或者没有链接成功
            if(this.socket.readyState === 3 || this.socket.readyState === 4) {
                if(this.isPauseConnet === false) {
                    this.socket = null
                }
            }
        }
        // callBack && callBack()
    }

    socketclose(){
        console.log("socketclose")
        if(this.socket && this.socket.readyState) {
            if(this.socket.readyState === 3 || this.socket.readyState === 4) {
                if(this.isPauseConnet === false) {
                    this.socket = null
                }
            }
        }
    }

    disconnect(){
        console.log("disconnect")
        this.isPauseConnet = false
        clearInterval(this.connectTimer)
        this.connectTimer = null
        if(this.socket){
            this.socket.close()
        }
    }

    checkConnect(){
        console.log("checkConnect")
        if(this.conentNum>3) {
            return 
        }
        if((new Date().getTime() - this.lastHeartBeat)>8000) {
            if(this.socket) {
                if(this.socket.readyState === 3 || this.socket.readyState===4) {
                    if(this.isPauseConnet === true) {
                        this.init()
                        this.conentNum ++ 
                    }
                }
            }else {
                if(this.isPauseConnet === true) {
                    this.init()
                    this.conentNum ++
                }
            }
        }else {
            let obj = {
                type: 'connect'
            }
            this.send(obj)
        }
    }
    // 订阅
    subscribeMsg(key, fn){
        if(this._subscribeMsgList[key]) {
            this._subscribeMsgList[key].push(fn)
        }else {
            this._subscribeMsgList[key] = [fn]
        }
    }
    // 取消订阅
    unsubscribe(key,fn){
        const fns = this._subscribeMsgList[key]
        if(!fns) {
            return
        }
        if(fn) {
            const index = fns.findIndx(e=>e===fn)
            fns.splice(index,1)
        } else {
            delete this._subscribeMsgList[key]
        }
    }
    // 通知
    _publishMsg(data,e){
       const keys = Object.keys(this._subscribeMsgList)
       for(let k of keys) {
        this._subscribeMsgList[k].forEach(fn => {
            if(typeof fn === 'function') {
                fn(data,e)
            }
        });
       }
    }
    // socketonmessage(callback) {
    //     console.log("socketonmessage")
    //     this.socket.onmessage = e => {
    //         console.log(e)
    //         let data = JSON.parse(e.data)
    //         this.lastHeartBeat = new Date().getTime()
    //         callback && callback(data)
    //     }
    // }

    send(data){
        if(this.socket !== null && this.socket.readyState === 1) {
            this.socket.send(JSON.stringify(data))
        }
    }
}

export default Socket1